package Dao;

import Entity.User;
import Mybatis.Mybatis;

import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDao implements UserDaoImpl {
    public List<User> findAllUser() {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.UserMapper.findAllUser";
        List<User> users = session.selectList(statement);
        session.close();
        return users;
    }
    public User findByNumber(String number) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.UserMapper.findByNumber";
        User user = session.selectOne(statement, number);
        session.close();
        return user;
    }
    public List<User> findByStatus(int status) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.UserMapper.findByStatus";
        List<User> users = session.selectList(statement, status);
        session.close();
        return users;
    }
    public boolean addUser(User user) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.UserMapper.addUser";
        int count = session.insert(statement, user);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean updateUser(User user) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.UserMapper.updateUser";
        int count = session.update(statement, user);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean deleteUser(User user) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.UserMapper.deleteUser";
        int count = session.delete(statement, user);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    
    
    //new add
    public User login(String username, String password) {
		  Mybatis mybatis = new Mybatis();
	      SqlSession session = mybatis.getSqlSession();
	      Map<String, Object> param=new HashMap<String, Object>();   
	      param.put("username", username);  
	      param.put("password",password);  
	      User user = (User)session.selectOne("mapper.UserMapper.login",param); 
	      System.out.println(user+" "+user.getName()+" "+user.getPassword());
	      session.close();
	      return user;
	}
    public User findByName(String name) {
		// TODO Auto-generated method stub
		Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();
        Map<String, Object> param=new HashMap<String, Object>();   
        param.put("name", name);  
        User user = (User)session.selectOne("mapper.UserMapper.findByName",param); 
        session.close();
        return user;
	}
    // ����
    public static void main(String[]args) {
        UserDao test = new UserDao();

        User user = new User("999", "С��", "999", 2);
        test.addUser(user);
        user.setName("С��");
        test.updateUser(user);
        test.deleteUser(user);
        List<User> users = test.findAllUser();
        for(int i = 0; i < users.size(); i++) {
            System.out.println(users.get(i).getNumber()+" "+users.get(i).getName()+" "+
                    users.get(i).getPassword()+" "+users.get(i).getStatus());
        }

        /*
        User user  = test.findByNumber("618");
        System.out.println(user.getNumber()+" "+user.getName()+" "+
                            user.getPassword()+" "+user.getStatus());
         */
        /*
        List<User> users = test.findByStatus(1);
        for(int i = 0; i < users.size(); i++) {
            System.out.println(users.get(i).getNumber()+" "+users.get(i).getName()+" "+
                    users.get(i).getPassword()+" "+users.get(i).getStatus());
        }
         */
    }
}
